package com.jincp.image.pick;

import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.jincp.image.pick.bean.Document;

import java.util.ArrayList;
import java.util.List;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.MediaColumns.DATA;

/**
 * Created by droidNinja on 01/08/16.
 */
public class DocScannerTask extends AsyncTask<Void, Void, List<Document>> {

    final String[] DOC_PROJECTION = {
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA,
            MediaStore.Files.FileColumns.MIME_TYPE,
            MediaStore.Files.FileColumns.SIZE,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Files.FileColumns.TITLE
    };
    private final OnDocumentLoadedListener resultCallback;

    String[] selectionArgs = new String[]{".pdf", ".ppt", ".pptx", ".xlsx", ".xls", ".doc", ".docx", ".txt"};

    private final Context context;

    public DocScannerTask(Context context, OnDocumentLoadedListener fileResultCallback) {
        this.context = context;
        this.resultCallback = fileResultCallback;
    }

    @Override
    protected List<Document> doInBackground(Void... voids) {
        ArrayList<Document> documents = new ArrayList<>();
        final String[] projection = DOC_PROJECTION;
        final Cursor cursor = context.getContentResolver().query(MediaStore.Files.getContentUri("external"),
                projection,
                null,
                null,
                MediaStore.Files.FileColumns.DATE_ADDED + " DESC");

        if (cursor != null) {
            documents = getDocumentFromCursor(cursor);
            cursor.close();
        }


        return documents;
    }

    @Override
    protected void onPostExecute(List<Document> documents) {
        super.onPostExecute(documents);
        if (resultCallback != null) {
            resultCallback.onDocumentLoaded(documents);
        }
    }

    private ArrayList<Document> getDocumentFromCursor(Cursor data) {
        if (data != null) {
            ArrayList<Document> documents = new ArrayList<>();
            while (data.moveToNext()) {

                int id = data.getInt(data.getColumnIndexOrThrow(_ID));
                String path = data.getString(data.getColumnIndexOrThrow(DATA));
                String title = data.getString(data.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
                long size = data.getLong(data.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE));

                if (path != null && contains(selectionArgs, path)) {
                    Document document = new Document(id, title, path, size);

                    String mimeType = data.getString(data.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE));
                    if (mimeType != null && !TextUtils.isEmpty(mimeType))
                        document.mimeType = mimeType;
                    else {
                        document.mimeType = "";
                    }
                    if (!documents.contains(document))
                        documents.add(document);
                }
            }
            return documents;
        }
        return null;
    }

    boolean contains(String[] types, String path) {
        for (String string : types) {
            if (path.endsWith(string)) return true;
        }
        return false;
    }

    /**
     * 所有图片加载完成的回调接口
     */
    public interface OnDocumentLoadedListener {
        void onDocumentLoaded(List<Document> documents);
    }
}
